//
//  XDHomeGradeViewController.m
//  XDEducation
//
//  Created by apple on 2017/4/7.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "XDHomeGradeViewController.h"
#import "XDGradeData.h"
#import "XDErrotsCampViewController.h"
#import "XDHomeCellHeaderView.h"
#import "XDHomeButtonItemCell.h"
#import "XDStarWarCell.h"
#import "XDHomeVideoCell.h"
#import "XDUnitCementSelectCourseViewController.h"
#import "XDVideoController.h"
#import "XDVideoModel.h"
#import "XDVideoPlayViewController.h"
#import "XDClassSynchronizationController.h"
#import "XDGradeVideoController.h"
@interface XDHomeGradeViewController ()

/** 页面数据 */
@property(nonatomic,strong) NSMutableArray *gradeDataArr;

/** 好成绩视频数据源 */
@property(nonatomic,strong) NSMutableArray <XDVideoModel *>*gradeVideoModelArr;
@end

static NSString *const practiceCellId = @"practiceCell";
static NSString *const starWarCellId = @"StarWarCell";
static NSString *const videoCellId = @"ViedoCell";
static NSString *const teacherCommentCellId = @"teacherCommentCell";

@implementation XDHomeGradeViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    [self initNetworkingWithRefreshStatus:RefreshStatePullDown];
    self.tableView.frame = CGRectMake(0, 44, ScreenW, ScreenH - 44 - 64 - 44);
}


-(NSMutableArray *)gradeDataArr{

    if (!_gradeDataArr) {
        _gradeDataArr = [NSMutableArray array];
        _gradeDataArr = [XDGradeData getGradeData];
    }
    return _gradeDataArr;
}

#pragma mark -tableview delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 4;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 5;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    NSString *title = self.gradeDataArr[section][@"title"];
    
    BOOL isShowMoreBtn = NO;
    
    if (section == 2) {
        
        isShowMoreBtn = YES;
    }
    
    XDHomeCellHeaderView *headerView =  [[XDHomeCellHeaderView alloc]initWithTitle:title MoreBtnStatus:isShowMoreBtn];
    WeakSelf(weakself)
    headerView.moreBlock = ^(){ //更多视频
        XDGradeVideoController *VC = [[XDGradeVideoController alloc] init];
        VC.XDGradeVideoControllerType = HaoChengJi;
        VC.title = title;
        [weakself.navigationController pushViewController:VC animated:YES];
    };
    return headerView;
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 44 * ScaleHeight;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    switch (indexPath.section) {
        case 0:
            return 100 * ScaleHeight;
            break;
         case 1:
            return 170 * ScaleHeight;
            break;
        case 2:
            return 170 * ScaleHeight;
            break;
        case 3:
            return 100 * ScaleHeight;
            break;
        default:
            return 0;
            break;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    WeakSelf(weakself)
    switch (indexPath.section) {
        case 0:{    //百炼成钢
            
            NSMutableArray *itemArr = self.gradeDataArr[0][@"item"];
            
            XDHomeButtonItemCell *cell = [XDHomeButtonItemCell cellWithTableView:tableView dataArr:itemArr cellId:practiceCellId];
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            NSString  *rolecode = [defaults objectForKey:@"rolecode"];
            if (0 == [rolecode integerValue]) {//超级管理员
                
            }else if (1 == [rolecode integerValue]){//管理员
                
            }else if (2 == [rolecode integerValue]){//任课老师
                
            }else if (3 == [rolecode integerValue]){//家长
                cell.itemBlock = ^(UIViewController *vc,NSString *title){
    
                    XDClassSynchronizationController *sys = [XDClassSynchronizationController new];
                    sys.nameStr = title;
                    [self.navigationController pushViewController:sys animated:YES];
                    
                };

            }else if (4 == [rolecode integerValue]){//孩子
                cell.itemBlock = ^(UIViewController *vc,NSString *title){
                    vc.title = title;
                    
                    [weakself.navigationController pushViewController:vc animated:YES];
                    
                };

            }

            
            
            return cell;
        }
            break;
        case 1:{    //全能星战
            
            [tableView registerNib:[UINib nibWithNibName:@"XDStarWarCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:starWarCellId];
            XDStarWarCell *cell = [tableView dequeueReusableCellWithIdentifier:starWarCellId];
            //错题集中营
            cell.errorsCampBlock = ^(){
                
                XDErrotsCampViewController *vc = [[XDErrotsCampViewController alloc]init];
                [weakself.navigationController pushViewController:vc animated:YES];
                
            };
            //单元巩固
            cell.uintCementBlock = ^(){
                XDUnitCementSelectCourseViewController *vc = [[XDUnitCementSelectCourseViewController alloc]init];
                vc.title = @"单元巩固";
                vc.selectCourseType = SelectCourseTypeUnitCement;
                [weakself.navigationController pushViewController:vc animated:YES];
            };
            //期中期末巩固
            cell.termCementBlock = ^(){
                XDUnitCementSelectCourseViewController *vc = [[XDUnitCementSelectCourseViewController alloc]init];
                vc.title = @"期中期末巩固";
                vc.selectCourseType = SelectCourseTypeTermCement;
                [weakself.navigationController pushViewController:vc animated:YES];
            };
            
            return cell;
        }
            break;
        case 2:{    //好成绩视频
        
            [tableView registerNib:[UINib nibWithNibName:@"XDHomeVideoCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:videoCellId];
            XDHomeVideoCell *cell = [tableView dequeueReusableCellWithIdentifier:videoCellId];
            cell.videoArr = self.gradeVideoModelArr;
            cell.videoClickedBlock = ^(XDVideoModel *model){
                if (model.absoluteUrl.length == 0) {
                    return ;
                }
                XDVideoPlayViewController *vc = [[XDVideoPlayViewController alloc]init];
                vc.videoModel = model;
                [weakself presentViewController:vc animated:YES completion:nil];
            };
            return cell;
        }
            break;
        case 3:{    //老师评语
            NSMutableArray *itemArr = self.gradeDataArr[3][@"item"];
            
            XDHomeButtonItemCell *cell = [XDHomeButtonItemCell cellWithTableView:tableView dataArr:itemArr cellId:teacherCommentCellId];
            
            cell.itemBlock = ^(UIViewController *vc,NSString *vcTitle){
                vc.title = vcTitle;
                
                [weakself.navigationController pushViewController:vc animated:YES];
            };
            return cell;
        }
            break;
        default:
            return nil;
            break;
    }
}


-(void)initNetworkingWithRefreshStatus:(RefreshState)status{
    
    //media.do
    //[page=1 | action=videolist | vtype=2 | type=0]

    //vtype=0 好成绩  vtype=1好习惯  vtype=2 好素养
    NSString *url = BaseURL(@"media.do?action=videolist");
    NSDictionary *dict = @{@"type":@0,@"vtype":@2,@"page":@1};
    [XDHttpTool get:url authCode:[AccountTool shareAccountTool].currentAccount.authCode params:dict success:^(id responseObj) {
       
        //NSLog(@"%@",responseObj);
        NSArray *dictArr = [responseObj objectForKey:@"dataList"];
        NSMutableArray *modelArr = [XDVideoModel mj_objectArrayWithKeyValuesArray:dictArr];
        self.gradeVideoModelArr = modelArr;
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
    
}

@end
